﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace PackingAndUnpacking
{
    public partial class _Default : System.Web.UI.Page
    {
        public int PackUnpackOrderID
        {
            get
            {
                if (ViewState["OrderID"] != null)
                    return (int)ViewState["OrderID"];
                else
                    return 0;
            }
            set
            {
                ViewState["OrderID"] = value;
            }
        }

        public string OrderAction
        {
            get
            {
                if (ViewState["OrderAction"] != null)
                    return ViewState["OrderAction"].ToString();
                else
                    return "GenerateOrder";
            }
            set
            {
                ViewState["OrderAction"] = value;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OrderAction = "GenerateOrder";                
                lableOrderGenerator.CssClass = "tool-label-active";
                ImgItemGenreted.ImageUrl = "~/images/Order_Generated.jpg";
            }
        }

        private void BindOrderID()
        {            
            ComboBoxOrderID.Items.Clear();            
            
            if (OrderAction == "CaseReturn")
                ComboBoxOrderID.DataSource = Reconciliation.Services.ListOfValues.GetReturnedOrder();
            else
                ComboBoxOrderID.DataSource = Reconciliation.Services.ListOfValues.GetGeneratedOrder();

            ComboBoxOrderID.DataTextField = "OrderID";
            ComboBoxOrderID.DataValueField = "OrderID";
            ComboBoxOrderID.DataBind();

            RadComboBoxItem item = new RadComboBoxItem();
            item.Value = "0";
            item.Text = "";
            ComboBoxOrderID.Items.Insert(0, item);

            ComboBoxOrderID.SelectedIndex = 0;
        }

        private void BindOrderList(int orderID)
        {
            pnlOrderDetailsArea.Visible = true;
            pnlOrderDetailsEdit.Visible = false;

            PackUnpackOrderID = orderID;
            //if (OrderAction == "CaseReturn")
            //    datalistDenom.DataSource = Reconciliation.Services.Services.GetPackagingOrderDetail(orderID);
            //else
            datalistDenom.DataSource = Reconciliation.Services.Services.GetPackagingOrderDetail(orderID);

            datalistDenom.DataBind();           
        }

      
        public void ComboBoxOrderID_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            int OrderID = int.Parse(((RadComboBox)o).SelectedValue);

            BindOrderList(OrderID);
            //GridDataInsertItem grdRow = (GridDataInsertItem)((RadComboBox)o).Parent.Parent;
            //FillDenomValues(OrderID, grdRow);
        }

        public void ComboBoxOrderID_TextChanged(object sender, EventArgs e)
        {
            int  OrderID = int.Parse(((RadComboBox)sender).SelectedValue);
            
            BindOrderList(OrderID);
            //GridDataInsertItem grdRow = (GridDataInsertItem)((RadComboBox)sender).Parent.Parent;
            //FillDenomValues(OrderID, grdRow);
            //RadNumericTextBox txtBox = new RadNumericTextBox();
            //txtBox = (RadNumericTextBox)grdRow.FindControl("tb10denom");
            //txtBox.Text = "800";
        }
                      
        protected void ImgItemGenreted_Click(object sender, ImageClickEventArgs e)
        {
            GenerateOrder();
        }
        protected void lableOrderGenerator_Click(object sender, EventArgs e)
        {
            GenerateOrder();
        }

        protected void ImageReturnedCase_Click(object sender, ImageClickEventArgs e)
        {
            ReturnCase();
        }

        protected void LinkReturnCase_Click(object sender, EventArgs e)
        {
            ReturnCase();
        }

        private void ReturnCase()
        {
            ComboBoxOrderID.Enabled = true;
            pnlOrderDetailsArea.Visible = true;
            pnlOrderDetailsEdit.Visible = false;

            OrderAction = "CaseReturn";
            lblMessage.Text = Resources.Lang.UnpackingOrder;
            BindOrderID();
            BindOrderList(0);
            //RadGrid1.MasterTableView.IsItemInserted = true;
            //RadGrid1.MasterTableView.Rebind();
            btnPackCase.ImageUrl = "~/images/Unpacked.jpg";
            lblpackcase.Text = Resources.Lang.UnPack_Order;
            lblMessage.ForeColor = System.Drawing.Color.Black;
            btnPackCase.Visible = true;
            lblpackcase.Visible = true;
            imgEditOrder.Visible = false; 

            LinkReturnCase.CssClass = "tool-label-active";
            ImageReturnedCase.ImageUrl = "~/images/Returned_Case.jpg";            

            // inactive all others
            Dot1.Attributes.Remove("class");
            Dot1.Attributes.Add("class", "dot-inactive");
            lableOrderGenerator.CssClass = "tool-label-inactive";
            ImgItemGenreted.ImageUrl = "~/images/Order_Generated1.jpg";

            //Order gererate images change
            Dot1.Attributes.Remove("class");
            Dot1.Attributes.Add("class", "dot-inactive");
            lableOrderGenerator.CssClass = "tool-label-inactive";
            lablePackCase.CssClass = "tool-label-inactive";                        
            ImgPackCase.ImageUrl = "~/images/Pack_Case1.jpg";
                        
            Dot2.Attributes.Remove("class");
            Dot2.Attributes.Add("class", "dot-inactive");
        }

        private void GenerateOrder()
        {
            ComboBoxOrderID.Enabled = true;
            pnlOrderDetailsArea.Visible = true;
            pnlOrderDetailsEdit.Visible = false;

            OrderAction = "GenerateOrder";
            lblMessage.Text = Resources.Lang.PackingOrder;
            lblMessage.ForeColor = System.Drawing.Color.Black;
            BindOrderID();
            BindOrderList(0);
            //RadGrid1.MasterTableView.IsItemInserted = true;
            //RadGrid1.Rebind();
            btnPackCase.ImageUrl = "~/images/Packed.jpg";
            lblpackcase.Text = Resources.Lang.Pack_Order;
            btnPackCase.Visible = true;
            lblpackcase.Visible = true;
            imgEditOrder.Visible = false; 

            lableOrderGenerator.CssClass = "tool-label-active";
            ImgItemGenreted.ImageUrl = "~/images/Order_Generated.jpg";
            
            // inactive all others
            LinkReturnCase.CssClass = "tool-label-inactive";
            ImageReturnedCase.ImageUrl = "~/images/Returned_Case1.jpg";

            //unpack case images change
            
            LinkReturnCase.CssClass = "tool-label-inactive";
            LinkUnpackCase.CssClass = "tool-label-inactive";
            ImageUnpackCase.ImageUrl = "~/images/Unpack_Case.jpg";

            lablePackCase.CssClass = "tool-label-inactive";
            ImgPackCase.ImageUrl = "~/images/Pack_Case1.jpg";
            Dot1.Attributes.Remove("class");
            Dot1.Attributes.Add("class", "dot-inactive");
            Dot2.Attributes.Remove("class");
            Dot2.Attributes.Add("class", "dot-inactive");
        }
        protected void lnkAddNewOrder_Click(object sender, EventArgs e)
        {
            lblMessage.ForeColor = System.Drawing.Color.Black;
            lableOrderGenerator.CssClass = "tool-label-active";
            ImgItemGenreted.ImageUrl = "~/images/Order_Generated.jpg";
            ImageUnpackCase.ImageUrl = "~/images/Unpack_Case.jpg";
            ImgPackCase.ImageUrl = "~/images/Pack_Case1.jpg";

            Dot1.Attributes.Remove("class");
            Dot1.Attributes.Add("class", "dot-inactive");
            Dot2.Attributes.Remove("class");
            Dot2.Attributes.Add("class", "dot-inactive");
            LinkReturnCase.CssClass = "tool-label-inactive";
            ImageReturnedCase.ImageUrl = "~/images/Returned_Case1.jpg";

            ComboBoxOrderID.Enabled = true;
            pnlOrderDetailsEdit.Visible = false;
            imgEditOrder.Visible = false;

            BindOrderID();
            btnPackCase.Visible = true;
            lblpackcase.Visible = true;
            lblpackcase.Text = Resources.Lang.Pack_Order;
            pnlOrderDetailsArea.Visible = true;           
            BindOrderList(0);

            if (OrderAction == "GenerateOrder")
            {
                lblMessage.Text = Resources.Lang.PackingOrder;
                lableOrderGenerator.CssClass = "tool-label-active";
                ImgItemGenreted.ImageUrl = "~/images/Order_Generated.jpg";
            }
            else
            {
                lblMessage.Text = Resources.Lang.UnpackingOrder;
                lableOrderGenerator.CssClass = "tool-label-inactive";
                ImgItemGenreted.ImageUrl = "~/images/Order_Generated1.jpg";
                LinkReturnCase.CssClass = "tool-label-active";
            }
        }

        private void GetPackedUnpackedOrders(int orderID)
        {
            pnlOrderDetailsArea.Visible = false;
            pnlOrderDetailsEdit.Visible = true;

            PackUnpackOrderID = orderID;

            lblOrderID.Text = orderID.ToString();
            if (OrderAction == "CaseReturn")
                binddatalist.DataSource = Reconciliation.Services.Services.GetPackagingOrderDetail(orderID);
            else
                binddatalist.DataSource = Reconciliation.Services.Services.GetPackagingOrderDetail(orderID);

            binddatalist.DataBind();
        }

        protected void btnPackCase_Click(object sender, ImageClickEventArgs e)
        {
            if (datalistDenom.Items.Count > 0)
            {
                try
                {
                    Reconciliation.Services.OrderDetail orderDetail = null;

                    List<Reconciliation.Services.OrderDetail> orderDetailList = new List<Reconciliation.Services.OrderDetail>();

                    int orderID = int.Parse(ComboBoxOrderID.SelectedValue);
                    foreach (DataListItem dli in datalistDenom.Items)
                    {
                        HiddenField hddDenomCode = (HiddenField)dli.FindControl("hiddDenomCode");
                        HiddenField hddDenomID = (HiddenField)dli.FindControl("hiddDenomID");
                        HiddenField hddOrderDetailID = (HiddenField)dli.FindControl("hiddOrderDetailID");
                        RadNumericTextBox txtDenomValue = (RadNumericTextBox)dli.FindControl("txtDenomValue");

                        orderDetail = new Reconciliation.Services.OrderDetail();
                        orderDetail.DenomID = int.Parse(hddDenomID.Value);
                        orderDetail.OrderDetailID = int.Parse(hddOrderDetailID.Value);         
                        orderDetail.OrderID = orderID;
                        orderDetail.DenomCode = hddDenomCode.Value;
                        orderDetail.OrderValue = Convert.ToDecimal(txtDenomValue.Text);
                        orderDetailList.Add(orderDetail);
                    }

                    if (OrderAction == "CaseReturn")
                    {
                        Reconciliation.Services.Services.SetUnPackagingOrderDetail(orderDetailList);
                        lblMessage.Text = Resources.Lang.UnPack_Success;
                        lblMessage.ForeColor = System.Drawing.Color.Green;

                        // change images
                        btnPackCase.Visible = false;
                        lblpackcase.Text = Resources.Lang.Edit_Order;
                        imgEditOrder.Visible = true;

                        Dot2.Attributes.Remove("class");
                        Dot2.Attributes.Add("class", "dot-active");
                        LinkReturnCase.CssClass = "tool-label-active";
                        LinkUnpackCase.CssClass = "tool-label-active";

                        ImageReturnedCase.ImageUrl = "~/images/Returned_Case.jpg";
                        ImageUnpackCase.ImageUrl = "~/images/Unpacked.jpg";
                    }
                    else
                    {
                        Reconciliation.Services.Services.SetPackagingOrderDetail(orderDetailList);
                        lblMessage.Text = Resources.Lang.Pack_Success;
                        lblMessage.ForeColor = System.Drawing.Color.Green;

                        // change images
                        btnPackCase.Visible = false;
                        lblpackcase.Text = Resources.Lang.Edit_Order;
                        imgEditOrder.Visible=true;

                        Dot1.Attributes.Remove("class");
                        Dot1.Attributes.Add("class", "dot-active");
                        lableOrderGenerator.CssClass = "tool-label-active";
                        lablePackCase.CssClass = "tool-label-active";

                        ImgItemGenreted.ImageUrl = "~/images/Order_Generated.jpg";
                        ImgPackCase.ImageUrl = "~/images/Pack_Case.jpg";
                    }

                    PackUnpackOrderID = orderID;
                    GetPackedUnpackedOrders(orderID);
                    
                }
                catch (Exception ex)
                {
                    lblMessage.Text = ex.Message;
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                }
            }
        }
        protected void imgEditOrder_Click(object sender, ImageClickEventArgs e)
        {
            // edit entry
            btnPackCase.Visible = true;            
            imgEditOrder.Visible = false;
            if (OrderAction == "CaseReturn")
                lblpackcase.Text = Resources.Lang.UnPack_Order;
            else
                lblpackcase.Text = Resources.Lang.Pack_Order;

            BindOrderList(PackUnpackOrderID);
            ComboBoxOrderID.Enabled = false;
        }
}
}
